﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerTruongDao : ITruongDao
    {
        public SqlServerTruongDao()
        {
            SQLUtility.TableName = "Truong";
        }

        //Trả về đối tượng báo ăn
        private Truong MakeTruong(DataRow row)
        {
            Guid _id=Guid.Parse(row["Id"].ToString());
            string _maTruong = row["MaTruong"].ToString();
            string _tenTruong = row["TenTruong"].ToString();
            string _xa = row["Xa"].ToString();
            string _huyen = row["Huyen"].ToString();
            string _tinh = row["Tinh"].ToString();


         return new Truong(_id, _maTruong, _tenTruong, _xa, _huyen, _tinh);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<Truong> MakeTruongs(DataTable dt)
        {
            IList<Truong> list = new List<Truong>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeTruong(row));
            }
            return list;
        }
        private DbParameter[] Take(Truong Truong)
        {
            DbParameter[] parameters = new DbParameter[6];
            parameters[0] = new SqlParameter("@Id", Truong.Id);
            parameters[1] = new SqlParameter("@MaTruong", Truong.MaTruong);
            parameters[2] = new SqlParameter("@TenTruong", Truong.TenTruong);
            parameters[3] = new SqlParameter("@Xa", Truong.Xa);
            parameters[4] = new SqlParameter("@Huyen", Truong.Huyen);
            parameters[5] = new SqlParameter("@Tinh", Truong.Tinh);
            
            return parameters;
        }


        public IList<Business_Objects.Truong> GetTruong()
        {
            SQLUtility.TableName = "Truong";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeTruongs(dt);
        }

        public Business_Objects.Truong GetTruongByID(string TruongID)
        {
            SQLUtility.TableName = "Truong";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@Id", TruongID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeTruong(row);
            }
        }

        public void InsertTruong(Business_Objects.Truong Truong)
        {
            SQLUtility.TableName = "Truong";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(Truong), true);
        }

        public int UpdateTruong(Business_Objects.Truong Truong)
        {
            SQLUtility.TableName = "Truong";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(Truong)); }
            catch { return -1; }
        }

        public int DeleteTruong(Business_Objects.Truong Truong)
        {
            SQLUtility.TableName = "Truong";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(Truong)); }
            catch { return -1; }
        }
    }
}
